package com.example.translateappjava.Service.ChatGPT;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentFactory;
import androidx.fragment.app.FragmentManager;

import com.example.translateappjava.Fragment.ShuaFragment;
import com.example.translateappjava.R;
import com.google.android.material.progressindicator.LinearProgressIndicator;

import java.io.IOException;
/**
 * @author gu
 * 这是是对请求方法封装到另外一个线程
 * 以实现请求,实现示例在MainActivity可见
 */
public class ChatGptHelper {

    private final Activity activity;
    private OnChatGptResponseListener listener;

    public ChatGptHelper(Activity activity) {
        this.activity = activity;
    }

    public interface OnChatGptResponseListener {
        void onChatGptResponse(String response);
        void onError(String message);
    }

    public void setOnChatGptResponseListener(OnChatGptResponseListener listener) {
        this.listener = listener;
    }

    public void chatGPTRequest(String question) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 在后台线程中执行网络请求
                    final String response = performNetworkRequest(question);
                    // 在 UI 线程中处理网络请求结果
                    activity.runOnUiThread(new Runnable() {
                        @SuppressLint("ResourceType")
                        @Override
                        public void run() {
                            Log.v("chatGPTRequest", response);
                            Log.v("question:", question);
                            // 在这里更新 UI
                            if (listener != null) {
                                String recognition = "";
                                if(question.contains("食物：")){
                                    recognition = question.substring(question.indexOf("食物："),question.indexOf(","));
                                }
                                listener.onChatGptResponse(recognition + response);
                            }

                        }
                    });
                } catch (IOException e) {
                    // 处理网络请求失败的情况
                    Log.e("Error", "Failed to fetch data", e);
                    if (listener != null) {
                        listener.onError("Failed to fetch data");
                    }
                }
            }
        }).start();
    }

    private String performNetworkRequest(String question) throws IOException {
        ChatGPTImplication chatGPTImplication = new ChatGPTImplication();
        return chatGPTImplication.getChatResponse(question);
    }

    private String parseGptResponse(String jsonResponse) {
        try {
            // 使用Android的JSON解析器
            org.json.JSONObject jsonObject = new org.json.JSONObject(jsonResponse);
            org.json.JSONArray choices = jsonObject.getJSONArray("choices");
            if (choices.length() > 0) {
                org.json.JSONObject firstChoice = choices.getJSONObject(0);
                org.json.JSONObject message = firstChoice.getJSONObject("message");
                return message.getString("content");
            }
        } catch (Exception e) {
            Log.e("ChatGptHelper", "Error parsing JSON response", e);
            return "解析响应失败: " + e.getMessage();
        }
        return "无有效响应";
    }
}